import { wxRequest } from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({ 
  data: { 
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    goodlist: wx.getStorageSync('goodsOdrList'),
    odrTotalPrice: wx.getStorageSync('odrTotalPrice'),
    note:'',
    defaultAddress:{},
    type:''
  }, 
  onLoad: function (option) {
    if (option.type) {
      this.setData({
        type: option.type
      })
    } 
    this.getDefaultAddress() 
  },
  onShow: function (options) {  
    this.setData({
      goodlist: wx.getStorageSync('goodsOdrList'),
      odrTotalPrice: wx.getStorageSync('odrTotalPrice')      
    }) 
  },

  // onUnload: function () {
  //   wx.removeStorageSync('goodsOdrList')
  // },

  // onHide: function () {
  //   wx.removeStorageSync('goodsOdrList')
  // },


  //监听input
  textareaBlur: function (e) { 
    var value = e.detail.value 
    this.setData({
      note:value
    })
  },

  //获取默认地址
  getDefaultAddress:function(){ 
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/customerAddress/defaultAddress',
      method: 'POST',
      data: {}
    }, (res) => {
       this.setData({
         defaultAddress: res.vo
       })
    })
  },

  //选择地址
  cheackAddress:function(){
    wx.navigateTo({
      url: '/pages/address/list?type=ord',
    })
  },
 


  //支付订单
  onClickButton:function(){ 
    this.odrRequest()
  },

  //订单请求
  odrRequest:function(){
    var that=this;
    if (!this.data.defaultAddress ) {
      wx.showToast({
        icon:'none',
        title: '请选择收货地址',
      })
      return false
    } 


    var params = {
      addId: this.data.defaultAddress.id,
      shoppingCartList: this.data.goodlist,
      note: this.data.note
    }

    //首单礼
    if(this.data.type==1){
      params.present=1
    } 
   
    console.log(params,'订单参数')
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/submitOrder',
      method: 'POST', 
      data: params
    }, (res) => {
      // wx.removeStorageSync('goodsOdrList')
      // wx.removeStorageSync('odrTotalPrice')
       console.log(res,'订单信息')

      var list=res.list
      var ids=list.map(item=>item.id) 
 
      // var id = res.vo.id
      // this.setData({
      //   id
      // })

      //订单金额大于0调取支付
      if (wx.getStorageSync('odrTotalPrice')>0){
        this.wxPay(ids)
      }else{
        wx.removeStorageSync('goodsOdrList')
        wx.removeStorageSync('odrTotalPrice')
        wx.showToast({
          icon: 'success',
          title: '下单成功',
        })
        setTimeout(function () {
          wx.redirectTo({
            url: '/pages/order/index?status=2',
          })
        }, 1000)
      } 
      
    })
  },


  //监听购物车数量
  handleCart(e) {
    var num = e.detail
    var key = e.target.dataset.key;
    var goodslist = this.data.goodlist
    var gdsItem = ''
    goodslist.map((item, index) => {
      if (index === key) {
        item.qty = num 
        gdsItem = {
            id: Number(item.id),
            gdsId: Number(item.gdsId),
            qty: item.qty
          }
      }
    })
    this.setData({
      shoppingCartList: goodslist
    }) 
    this.addCate(gdsItem)
    this.hendleTotalPrice()
  },

  //加入购物车
  addCate: function (params) {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/comm/beanCrud/mall.ShoppingCartVO/update',
      method: 'post',
      data: params
    }, (res) => {
      // wx.showToast({
      //   title: '加入购物车成功',
      // })
    })
  },




  hendleTotalPrice() { //计算总价
    let that = this
    let price = 0
    for (let i = 0; i < that.data.shoppingCartList.length; i++) {
      // if (that.data.shoppingCartList[i].checked) {
        price = price + that.data.shoppingCartList[i].price * that.data.shoppingCartList[i].qty
      // }
    }
    price = price.toFixed(2)
    this.setData({
      odrTotalPrice: price
    })
  },
 



//微信支付  
  wxPay: function (odrIdList) {
    var that = this; 
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/wxPay',
      method: 'POST',
      data: odrIdList,
    }, function (res) { 
      that.requestPayment(res)
    })
  },

 


 //微信支付
  requestPayment: function (data) {
    var that = this;
    wx.requestPayment({
      'timeStamp': data.timeStamp,
      'nonceStr': data.nonceStr,
      'package': data.pkg,
      'signType': data.signType,
      'paySign': data.paySign,
      'success': function (res) {
        wx.removeStorageSync('goodsOdrList')
        wx.removeStorageSync('odrTotalPrice')
        wx.showToast({
          icon: 'none',
          title: '支付成功',
        })
        setTimeout(function () {
          wx.redirectTo({
            // url: '/pages/settle/success?id=' + that.data.id,
            url:'/pages/order/index?status=2'
          })
        }, 1000)
      },
      'fail': function (res) {
        wx.showToast({
          icon: 'none',
          title: '支付失败',
        })
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/order/index',
          })
        }, 1500)
      },
      'complete': function (res) {
        // that.payCallBack(data.outTradeNo)
      }
    })
  },



//   payCallBack: function (outTradeNo) {
//     var that = this;
//     _request.u_Ajax({
//       url: app.globalData.apiUrl + '/wechat/payCallBack',
//       method: 'POST',
//       data: {
//         outTradeNo
//       },
//     }, function (res) {
//       console.log(outTradeNo)
//     })
//   }






   
})